package file.io;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import model.Aggregation;
import model.Association;
import model.Attribute;
import model.ClassObj;
import model.Inheritance;

public class FileWrite {
	FileOutputStream fos;
	DataOutputStream dos;
	

	/**
	 * write the output to the file in the specific format
	 */
	public void writeClassAttribute(ArrayList<ClassObj> classList) {
		FileOutputStream fos_attr;
		FileOutputStream fos_method;
		DataOutputStream dos_attr;
		DataOutputStream dos_method;
		
		String classname;
		
		try {
			File class_file = new File("tmp\\OOT_Classname");
			File attr_file = new File("tmp\\OOT_Attrib");
			File method_file = new File("tmp\\OOT_Method");
			fos = new FileOutputStream(class_file);
			dos = new DataOutputStream(fos);
			fos_attr = new FileOutputStream(attr_file);
			dos_attr = new DataOutputStream(fos_attr);
			fos_method = new FileOutputStream(method_file);
			dos_method = new DataOutputStream(fos_method);
			

			for (ClassObj obj : classList) {
				classname = obj.getName();
				dos.writeBytes(classname + " C 1 1 t\n");
				dos_method.writeBytes(classname + " <PACKAGE> 0\n");				
				dos_attr.writeBytes(classname + " <PACKAGE> "
						+ obj.getAttributes().size() + "\n");
				
				// OOT requires a type for the attributes, so 
				// assume all types are String
				for (Attribute attr : obj.getAttributes()) {
					dos_attr.writeBytes(attr.getName() + " String\n");
				}
			}
			dos.close();
			dos_attr.close();
			dos_method.close();
			fos.close();
			fos_attr.close();
			fos_method.close();
		} catch (IOException e) {
			System.out.println("Error writing to the file");
		}
	}

	public void writeRelation(ArrayList<Inheritance> inheritanceList,
			ArrayList<Association> associationList,
			ArrayList<Aggregation> aggregationList) {

		try {
			File relation_file = new File("tmp\\OOT_Relation_File");
			fos = new FileOutputStream(relation_file);
			dos = new DataOutputStream(fos);

			for (Inheritance inh : inheritanceList) {
				dos.writeBytes(inh.getDstClass().getName() + " "
						+ inh.getSrcClass().getName() + " I\n");
			}
			for (Aggregation agg : aggregationList) {
				dos.writeBytes(agg.getSrcClass().getName() + " "
						+ agg.getDstClass().getName() + " A\n");
			}
			for (Association assoc : associationList) {
				dos.writeBytes(assoc.getSrcClass().getName() + " "
						+ assoc.getDestClass().getName() + " F 0\n");
			}
			dos.close();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
